package com.qinghe.pro.spring;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import com.qinghe.utils.AjaxObject;

/**
 * 返回带错误信息和状态码的Result字符串.
 * 
 */
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {

	@InitBinder
	public void dataBinder(WebDataBinder dataBinder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
	}

	@ExceptionHandler(value = {MaxUploadSizeExceededException.class})
    public ResponseEntity<String> maxUploadSizeExceededException(MaxUploadSizeExceededException e) {
		return new ResponseEntity<String>(AjaxObject.newError("文件大小限制1G.").toString(), HttpStatus.OK);
	}

}
